package com.spzx.product.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.spzx.common.core.web.domain.AjaxResult;
import com.spzx.common.core.web.page.TableDataInfo;
import com.spzx.common.security.utils.SecurityUtils;
import com.spzx.product.domain.Brand;
import com.spzx.product.service.IBrandService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.spzx.common.core.web.controller.BaseController;

import java.util.List;

/**
 * <p>
 * 分类品牌 前端控制器
 * </p>
 *
 * @author atguigu
 * @since 2025-08-11
 */
@RestController
@RequestMapping("/brand")
public class BrandController extends BaseController {

    @Autowired
    private IBrandService brandService;

    /**
     * 分页查询品牌列表
     * @param name 查询条件
     * @return 若依前端框架需要分页对象
     */
    @Operation(summary = "查询品牌列表")
    @GetMapping("list")
    public TableDataInfo list(@Parameter(description = "品牌名称") String name){
        //1. 开始分页 底层封装分页插件PageHelper
        startPage();
        //2. 调用业务层根据条件查询品牌列表
        List<Brand> list = brandService.selectBrandList(name);
        //3. 基于查询到业务列表数据封装分页对象
        return getDataTable(list);
    }

    /**
     * 根据品牌ID查询品牌信息
     * @param id
     * @return
     */
    @Operation(summary = "获取品牌详细信息")
    @GetMapping("/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id){
        //方式一：
        //Brand brand = brandService.getById(id);
        //方式二：
        LambdaQueryWrapper<Brand> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Brand::getId,id);
        queryWrapper.select(Brand::getId,Brand::getName,Brand::getLogo);
        Brand brand = brandService.getOne(queryWrapper);
        return AjaxResult.success(brand);
    }


    /**
     * 新增品牌
     * @param brand
     * @return
     */
    @Operation(summary = "新增品牌")
    @PostMapping
    public AjaxResult add(@Parameter(description = "品牌") @RequestBody Brand brand){
        brandService.add(brand);
        return AjaxResult.success();
    }

    /**
     * 修改品牌
     * @param brand
     * @return
     */
    @Operation(summary = "修改品牌")
    @PutMapping
    public AjaxResult edit(@Parameter(description = "品牌") @RequestBody Brand brand){
        brand.setUpdateBy(SecurityUtils.getUsername());
        brandService.updateById(brand);
        return AjaxResult.success();
    }

    /**
     * 删除品牌 （未删除：0 已删除：2）
     * @param ids
     * @return
     */
    @Operation(summary = "删除品牌")
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@Parameter(description = "品牌id列表", example = "[1,2]") @PathVariable List<Long> ids){
        brandService.removeBatchByIds(ids);
        return AjaxResult.success();
    }

    /**
     * 查询所有品牌列表
     * @return
     */
    @Operation(summary = "查询所有品牌列表")
    @GetMapping("/getBrandAll")
    public AjaxResult getBrandAll(){
        List<Brand> list = brandService.list();
        return AjaxResult.success(list);
    }

}

